fix: add option to force keep_trace_attributes in langfuse #1434
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix of the bug, when langfuse trace attributes was not passed to Langfuse UI, if call was inside of LLM chain
langfuse/langfuse#10380
Important
Fix bug in
CallbackHandler.pyto ensure trace attributes are passed to Langfuse UI when LLM is run inside a chain.CallbackHandler.py, fix bug in__on_llm_actionwhere trace attributes were not passed to Langfuse UI if call was inside LLM chain.keep_trace_attributesto determine if trace attributes should be kept based onmetadataor ifparent_run_idisNone.This description was created by
for 3d36dfc. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Greptile Summary
Adds
keep_trace_attributesoption to force preservation of Langfuse trace attributes (session ID, user ID, tags) when LLM calls occur inside chainsparent_run_id is None)metadata['keep_trace_attributes']to preserve attributes in chain contextsAttributeErrorwhenmetadataisNonesince.get()is called on optional parameterConfidence Score: 2/5
metadataisNoneAttributeErroron line 796 whenmetadataisNone(calling.get()onNone). This is a guaranteed runtime error that must be fixed before merging.Nonehandling bugImportant Files Changed
File Analysis
keep_trace_attributesflag to allow forcing trace attributes in LLM chains, but containsAttributeErrorbug whenmetadataisNoneSequence Diagram
sequenceDiagram participant User participant LangchainCallbackHandler participant __on_llm_action participant __join_tags_and_metadata participant _strip_langfuse_keys_from_dict participant LangfuseObservation User->>LangchainCallbackHandler: on_llm_start(metadata={keep_trace_attributes: true}) LangchainCallbackHandler->>__on_llm_action: Process LLM start event alt metadata.keep_trace_attributes == true __on_llm_action->>__on_llm_action: keep_trace_attributes = True else parent_run_id is None (isolated LLM call) __on_llm_action->>__on_llm_action: keep_trace_attributes = True else inside chain __on_llm_action->>__on_llm_action: keep_trace_attributes = False end __on_llm_action->>__join_tags_and_metadata: Join metadata with keep flag __join_tags_and_metadata->>_strip_langfuse_keys_from_dict: Strip keys conditionally alt keep_trace_attributes == True _strip_langfuse_keys_from_dict->>_strip_langfuse_keys_from_dict: Keep langfuse_session_id, langfuse_user_id, langfuse_tags else keep_trace_attributes == False _strip_langfuse_keys_from_dict->>_strip_langfuse_keys_from_dict: Strip langfuse_session_id, langfuse_user_id, langfuse_tags end _strip_langfuse_keys_from_dict-->>__on_llm_action: Return processed metadata __on_llm_action->>LangfuseObservation: start_observation(metadata=processed_metadata) LangfuseObservation-->>User: Trace created with/without attributes